<?php
namespace app\yunda\model;

use think\Model;
use traits\model\SoftDelete;

/**
 * 佣金
 * @package app\yunda\model
 */
class Brokerage extends Model
{
    use SoftDelete;
    protected $table = 'dp_yd_brokerage';
    protected $autoWriteTimestamp = true;

    /**
     * 计算当前用户消费上级所获佣金并存入记录表
     * @param string $openid
     * @param float $pay
     * @return bool|string
     */
    public static function getParentMoney($openid='',$pay=0.00){
        if(empty($openid)) return $res='消费用户不能为空';
        $user=User::get(['openid'=>$openid]);
        if(empty($user)) return $res='消费用户不存在';

        //获取当前消费用户的第一级
        if(isset($user['one_leader']) && !empty($user['one_leader'])){
            $one_leader=User::get($user['one_leader']);
            if(!empty($one_leader)){
                if($user['member_type']==0){//非会员
                    $yj=self::getYj();
                }else{
                    $yj=self::getYj(1,$user['member_type']);
                }
                $yongjin=round(($pay*$yj)/100,2);
                $money=$one_leader['money']+$yongjin;
                $one_leader->update(['money'=>$money,'id'=>$user['one_leader']]);
                $res=Yjrecord::create([
                    'openid'=>$one_leader['openid'],//获得佣金用户
                    'pay_user_openid'=>$openid,//消费用户
                    'get_money'=>$yongjin,//获得佣金
                    'level'    =>1,
                    'pay_money'=>$pay,
                ]);
            }

        }
        //获取当前消费用户的第二级
        if(isset($user['two_leader']) && !empty($user['two_leader'])){
            $two_leader=User::get($user['two_leader']);
            if(!empty($two_leader)){
                if($user['member_type']==0){//非会员
                    $yj=self::getYj(2);
                }else{
                    $yj=self::getYj(2,$user['member_type']);
                }
                $yongjin=round(($pay*$yj)/100,2);
                $money=$two_leader['money']+$yongjin;
                $two_leader->update(['money'=>$money,'id'=>$user['two_leader']]);
                $res=Yjrecord::create([
                    'openid'=>$two_leader['openid'],//获得佣金用户
                    'pay_user_openid'=>$openid,//消费用户
                    'get_money'=>$yongjin,//获得佣金
                    'level'    =>2,
                    'pay_money'=>$pay,
                ]);
            }
        }
        //获取当前消费用户的第三级
        if(isset($user['three_leader']) && !empty($user['three_leader'])){
            $three_leader=User::get($user['three_leader']);
            if(!empty($three_leader)){
                if($user['member_type']==0){//非会员
                    $yj=self::getYj(3);
                }else{
                    $yj=self::getYj(3,$user['member_type']);
                }
                $yongjin=round(($pay*$yj)/100,2);
                $money=$three_leader['money']+$yongjin;
                $three_leader->update(['money'=>$money,'id'=>$user['three_leader']]);
                $res=Yjrecord::create([
                'openid'=>$three_leader['openid'],//获得佣金用户
                'pay_user_openid'=>$openid,//消费用户
                'get_money'=>$yongjin,//获得佣金
                'level'    =>3,//佣金级别
                'pay_money'=>$pay,//消费金额
            ]);
            }

        }
        return isset($res) ? $res :'当前消费用户没有绑定上级';
    }

    /**
     * 获取佣金设置
     * @param int $level
     * @return bool|mixed
     */
    public static function getYj($level=1,$hy_level=0){

        $yongjin=self::get(['level'=>$level,'hy_level'=>$hy_level]);
        if(!empty($yongjin)) return $yongjin->money;
        return false;
    }
}
